package com.cn.wanxi.data.impl;

import com.cn.wanxi.data.UserData;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements UserData {
    @Override
    public int register(UserModel userModel) {
        String sql = "INSERT INTO `sys`.`sys_user` \n" +
                "( `username`," +
                " `password`," +
                " `phone`," +
                " `weight`," +
                " `email`," +
                " `sex`," +
                " `hobby`," +
                " `birthday`," +
                "`create_time`)\n" +
                " VALUES ( '" + userModel.getUsername() + "'," +
                " '" + userModel.getPassword() + "'," +
                " '" + userModel.getPhone() + "'," +
                " '" + userModel.getWeight() + "'," +
                " '" + userModel.getEmail() + "'," +
                " '" + userModel.getSex() + "'," +
                " '" + userModel.getHobby() + "'," +
                " '" + userModel.getBirthday() + "'," +
                "now())";


        return JDBC.excute(sql);

//1.13.4
    }

    @Override
    public UserModel findByUsername(String username) {
        String sql = "select * from sys.sys_user where username='" + username + "'";
        ResultSet resultSet = JDBC.query(sql);
        UserModel userModel = null;

        try {
            while (resultSet.next()) {
                userModel = new UserModel();

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }


        return userModel;
    }

    @Override
    public UserModel findByUsernameAndPassword(UserModel userModel) {
        String sql = "select * from sys_user " +
                "where username='" + userModel.getUsername() + "' and " +
                "password='" + userModel.getPassword() + "'";
        ResultSet resultSet = JDBC.query(sql);
        UserModel model = null;

        try {
            while (resultSet.next()) {
                model = new UserModel();

            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {
        String sql = "select * from sys_user " +
                "where true ";
        if (userModel.getUsername() != null && !"".equals(userModel.getUsername())) {
            sql += "and username like '%" + userModel.getUsername() + "%' ";
        }
        if (userModel.getPhone() != null && !"".equals(userModel.getPhone())) {
            sql += "and phone like '%" + userModel.getPhone() + "%'";
        }
        if (userModel.getEmail() != null && !"".equals(userModel.getEmail())) {
            sql += "and email like '%" + userModel.getEmail() + "%'";
        }
        if (!"".equals(userModel.getHobby())) {
            String[] hobbys = userModel.getHobby().split(",");
            for (int i = 0; i < hobbys.length; i++) {
                sql += " and FIND_IN_SET('" + hobbys[i] + "',hobby)";
            }
        }
//        if (userModel.getHobby() != null && !"".equals(userModel.getHobby())) {
//            sql += "and hobby like '%" + userModel.getHobby() + "%'";
//        }
        if (userModel.getSex() != null && !"全部".equals(userModel.getSex())) {
            sql += "and sex like '%" + userModel.getSex() + "%'";
        }
        if (!"".equals(userModel.getStartBirthday()) && "".equals(userModel.getEndBirthday())) {
            sql += " and birthday >='" + userModel.getStartBirthday() + "'";
        }
        if ("".equals(userModel.getStartBirthday()) && !"".equals(userModel.getEndBirthday())) {
            sql += " and birthday <='" + userModel.getEndBirthday() + "'";
        }
        if (!"".equals(userModel.getStartBirthday()) && !"".equals(userModel.getEndBirthday())) {
            sql += " and birthday between '" + userModel.getStartBirthday() + "' and '" + userModel.getEndBirthday() + "'";
        }
//        if (userModel.getEnable() != -1 && userModel.getEnable() != null){
//            if (userModel.getEnable() == 1){
//                sql +=" and enable=1";
//            }else{
//                sql +="and enable!=1";
//            }
//        }
        sql += "order by id desc";
        ResultSet resultSet = JDBC.query(sql);
        return setResultData(resultSet);
    }

    @Override
    public int del(Integer id) {
        String sql = "delete from sys_user where id=" + id;

        return JDBC.excute(sql);
    }

    @Override
    public UserModel findById(Integer id) {
        String sql = "select * from sys_user where id=" + id;
        ResultSet resultSet = JDBC.query(sql);
        List<UserModel> list = setResultData(resultSet);
        return list.size() > 0 ? list.get(0) : null;
    }

    @Override
    public int edit(UserModel userModel) {
        String sql = "update user set username='" + userModel.getUsername() + "'where id =" + userModel.getId();
        return JDBC.excute(sql);
    }

    @Override
    public UserModel findByUsernameForEdit(UserModel userModel) {
        String sql = "select * from sys_user where username='" + userModel.getUsername() + "'and id !=" + userModel.getId();
        ResultSet resultSet = JDBC.query(sql);
        List<UserModel> list = setResultData(resultSet);
        return list.size() > 0 ? list.get(0) : null;
    }

    //放置数据到sql方法
    private List<UserModel> setResultData(ResultSet resultSet) {
        List<UserModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                UserModel model = new UserModel();
                model.setId(resultSet.getInt("id"));
                model.setUsername(resultSet.getString("username"));
                model.setPhone(resultSet.getString("phone"));
                model.setEmail(resultSet.getString("email"));
                model.setHobby(resultSet.getString("hobby"));
                model.setSex(resultSet.getString("sex"));
                model.setEnable(resultSet.getInt("enable"));
                model.setBirthday(resultSet.getString("birthday"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

}

